using System.Collections.Generic;
using UnityEngine;

namespace Gameplay.PVE
{
    public class MoveStage : StageBase
    {
        private List<Vector3> playerPositions = new List<Vector3>();
        private List<Vector3> subPlayerPositions = new List<Vector3>();
        private List<Vector3> playerForwards = new List<Vector3>();
        private List<Vector3> subPlayerForwards = new List<Vector3>();

        private List<bool> playerCheckList = new List<bool>();
        private List<bool> subPlayerCheckList = new List<bool>();

        protected override void InitData()
        {
            if (id == 0)
            {
                playerPositions = new List<Vector3>
                {
                    players[0].Data.position,
                    players[0].Data.position + new Vector3(-4f,0,-2f),
                    players[0].Data.position + new Vector3(-4f,0,2f),
                };
                playerForwards = new List<Vector3>
                {
                    new Vector3(84f - 100,0,93.8f - 100) + PveManager.Instance.GetStagePosition() - players[0].Data.position,
                    new Vector3(82.7f - 100,0,96.3f - 100) + PveManager.Instance.GetStagePosition() - players[1].Data.position,
                    new Vector3(85.3f - 100,0,96.7f - 100) + PveManager.Instance.GetStagePosition() - players[2].Data.position,
                };
            }
            else if (id == 1)
            {
                playerPositions = new List<Vector3>
                {
                    new Vector3(84f - 100,0,93.8f - 100) + PveManager.Instance.GetStagePosition(),
                    new Vector3(82.7f - 100,0,96.3f - 100) + PveManager.Instance.GetStagePosition(),
                    new Vector3(85.3f - 100,0,96.7f - 100) + PveManager.Instance.GetStagePosition(),
                };
                playerForwards = new List<Vector3>
                {
                    new Vector3(0,0,-1),
                    new Vector3(0,0,-1),
                    new Vector3(0,0,-1),
                };
            }
            else if (id == 3)
            {
                playerPositions = new List<Vector3>()
                {
                    new Vector3(84 - 100,0,100-100) + PveManager.Instance.GetStagePosition(),
                    new Vector3(81.78f - 100,0,98.42f - 100f) + PveManager.Instance.GetStagePosition(),
                    new Vector3(81.78f - 100, 0, 101.2f - 100f) + PveManager.Instance.GetStagePosition(),
                };
                playerForwards = new List<Vector3>
                {
                    new Vector3(1,0,0),
                    new Vector3(1,0,0),
                    new Vector3(1,0,0),
                };
            }
            for (int i = 0; i < players.Count; i++)
            {
                players[i].Data.SetTargetPosition(playerPositions[i]);
            }
            playerCheckList.Clear();
            for (int i = 0; i < players.Count; i++)
            {
                playerCheckList.Add(false);
            }
            subPlayerCheckList.Clear();
            for (int i = 0; i < subPlayers.Count; i++)
            {
                subPlayerCheckList.Add(false);
            }
            playerCheckList.Add(false);
        }

        public override void Update()
        {
            base.Update();
            var isFinish = true;
            for (int i = 0; i < players.Count; i++)
            {
                if (players[i].Data.position == playerPositions[i])
                {
                    if (!playerCheckList[i])
                    {
                        playerCheckList[i] = true;
                        players[i].Play("Idle");
                        players[i].Data.SetTargetForward(playerForwards[i]);
                    }
                }
                else
                {
                    isFinish = false;
                }
            }
            this.isActionFinished = isFinish;
        }
    }
}